import { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { useFileWatcher, importConfig, transporter, getGitLog, gitPush, incrementVersion, modifyJson } from '@xuekl/cli-panel'
import viteCompression from 'vite-plugin-compression'
import TranslateLineColor from './plugins/translate-line-color'
import proxy from './proxy'
import { version, name } from './package.json'
import mom from 'dayjs'
const env = process.env.NODE_ENV


const plugins = [
  importConfig(),
  TranslateLineColor(),
  // TestPlugin(),
  vue(),

  createSvgIconsPlugin({
    // 指定需要缓存的图标文件夹
    iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
    // 指定symbolId格式
    symbolId: '[name]',
  }),

]

if (env === 'development') {
  plugins.push(useFileWatcher(resolve(__dirname, "src/views/module")))
} else {
  const users = [
    '1411213057@qq.com',
    // '212948038@qq.com',
    // '1063152386@qq.com',
    // '1910417382@qq.com',
    // '1715390727@qq.com',
    // '649175125@qq.com',
    // '247087563@qq.com',
    // '1836889297@qq.com'
  ]
  const updateVersion = incrementVersion(version)
  modifyJson(resolve(__dirname, './package.json'), 'version', updateVersion)
  getGitLog((logs) => {
    // 发送邮件 
    transporter.sendMail({
      from: `"薛康林友情提醒"<1411213057@qq.com>`,
      subject: `v${updateVersion}发布通知`,
      to: users.join(','),
      html: `
<h1>${name} 前端<a href="http://10.32.31.19:30821/#/login">v${updateVersion}</a>已发布，请及时验证上新功能</h1>
<ul>
      ${logs.filter(log => !!log).map(log => '<li>' + log + '</li>').join('')}
</ul>
<span style="color: green">发布时间：${mom().format('YYYY-MM-DD HH:mm')}</span>
`
    }, (error, info) => {
      if (error) {
        return console.log('发送失败:', error);
      }
      transporter.close()
      console.log('\n邮件--发送成功');
    })
  }, 10)
  plugins.push(
    viteCompression()
  )
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins,
  // base: '/controltower/',
  base: '/',
  resolve: {
    alias: [
      {
        find: '@',
        replacement: resolve(__dirname, "src")
      },
    ]
  },
  server: {
    proxy
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 定义全局的scss变量
        // 给导入的路径最后加上 ; 
        additionalData: `@import '@/assets/scss/_variable.scss';`
      }
    }
  }
})
